package org.lboot.rbac.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.lboot.core.domain.ResponseDTO;
import org.lboot.rbac.module.user.SysUser;
import org.lboot.rbac.module.user.SysUserService;
import org.lboot.rbac.module.user.params.ProfileUpdateParams;
import org.lboot.rbac.util.RbacUtil;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author kindear
 * 用户个人信息修改接口
 */
@RestController
@RequestMapping("system")
@Api(tags = "个人信息")
@AllArgsConstructor
public class ProfileController {
    SysUserService userService;

    @PutMapping("user/profile")
    @ApiOperation(value = "更新个人信息")
    public ResponseDTO<Object> updateUserProfile(@RequestBody ProfileUpdateParams params){
        // 获取当前登录用户
        Long userId = StpUtil.getLoginIdAsLong();
        SysUser user = userService.getOneUnwrap(userId);
        // 参数覆盖
        BeanUtil.copyProperties(params,user, CopyOptions.create().setIgnoreError(true).setIgnoreNullValue(true));
        SysUser result = userService.updateOne(user);
        // 数据脱敏后返回
        return ResponseDTO.succData(RbacUtil.desensitize(result));
    }
    @GetMapping("user/profile")
    @ApiOperation(value = "用户个人信息")
    public ResponseDTO<Object> queryUserProfile(){
        // 获取用户登录ID
        Long uId = StpUtil.getLoginIdAsLong();
        SysUser user = userService.getOneUnwrap(uId);
        return ResponseDTO.succData(RbacUtil.desensitize(user));
    }
}
